package com.jaws.core.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.CaseFormat;

/**
 * 字符串操作工具类(类似于commons-lang3里面的StringUtils)
 *
 * @author jiangmy
 * @date 2016-08-03 18:41:56
 * @since v1.0.0
 * @see StringUtils
 * @see StringEscapeUtils
 * @see CaseFormat
 */
public class StringUtil {
	static Logger logger = LoggerFactory.getLogger(StringUtil.class);

	/**
	 * 例如：HelloWorld->hello_world
	 * 下划线连接(数据库字段名)
	 * 
	 * @param name 转换前的驼峰式命名的字符串
	 * @return 转换后下划线大写方式命名的字符串
	 */
	public static String underscore(String name) {
		return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name);
	}

	/**
	 * 例如：HELLO_WORLD->HelloWorld
	 * 首字母大写的驼峰格式(类名)
	 * 
	 * @param name 转换前的下划线大写方式命名的字符串
	 * @return 转换后的驼峰式命名的字符串
	 */
	public static String camelName(String name) {
		return StringUtils.capitalize(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name));
	}

	/**
	 * 例如：HELLO_WORLD->helloWorld
	 * 首字母小写的驼峰格式(属性名)
	 * 
	 * @param name 转换前的下划线大写方式命名的字符串
	 * @return 转换后的驼峰式命名的字符串
	 */
	public static String camelProperty(String name) {
		return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
	}

	/**
	 * 获取字符串中正整数
	 *
	 * @param str
	 * @return
	 * @author jiangmy
	 * @date 2016-06-07 08:46:49
	 */
	public static int getNumFromStr(String str) {
		String result = "";
		if (StringUtils.isNoneBlank(str)) {
			for (int i = 0; i < str.length(); i++) {
				if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
					result += str.charAt(i);
				}
			}
		}
		try {
			return Integer.valueOf(result);
		} catch (Exception e) {
			return -1;
		}
	}

	/**
	 * 参数过滤urlEncode
	 * 
	 * @author jiangmy
	 * @date 2017年5月18日 下午3:06:01
	 * @since v1.0.0
	 *
	 * @param s
	 * @return
	 */
	public static String urlParameterFilter(String s) {
		if (StringUtils.isBlank(s)) {
			return s;
		}
		s = s.trim();
		int i = 5;
		while (StringUtils.contains(s, "%") && i-- > 0) {
			try {
				s = URLDecoder.decode(s, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return s;
	}
	
	/**
	 * 判断是否为中文字符
	 */
	public static boolean isChinese(String str) {
		for (char c : str.toCharArray()) {
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
			if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
					|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
					|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
					|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
			} else {
				return false;
			}
		}
		return true;
	}
	
	public static boolean hasChinese(String str) {
		for (char c : str.toCharArray()) {
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
			if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
					|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
					|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
					|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
				return true;
			}
		}
		return false;
	}
}
